package com.woniu.user.controller;


import com.woniu.common.result.Result;
import com.woniu.user.util.MinioClientUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

@RequestMapping("/user/file")
@RestController
public class FileUpdateController {
    @Resource
    private MinioClientUtil minioClient;
//    @Value("${minio.endpoint}")
//    private String ep;
//    @Value("${minio.bucketName}")
//    private String bn;
//    @Value("${minio.port}")
//    private String pt;


    @PostMapping("/upload")
    public Result minioUpdate(@RequestPart MultipartFile imgFile) throws Exception {
//        String url="http://"+ep+":"+pt+"/"+bn+"/";
        /*上传成功返回图片的路径*/
        /*获取原始文件名称*/
        String oldFileName=imgFile.getOriginalFilename();
       /*使用UUID生成新的文件名称*/
        String newFileName=UUID.randomUUID().toString()+oldFileName.substring(oldFileName.lastIndexOf("."));
        boolean flag=minioClient.putObject(newFileName,imgFile.getInputStream());
//        newFileName=url+newFileName;
        if(flag){
            return Result.ok(newFileName);
        }
        return Result.error();
    }

    @PostMapping("/uploadVideo")
    public Result minioUpdateVideo(@RequestPart MultipartFile videoFile) throws Exception {
//        String url="http://"+ep+":"+pt+"/"+bn+"/";
        /*上传成功返回图片的路径*/
        String newFileUrl="";
        /*获取原始文件名称*/
        String oldFileName=videoFile.getOriginalFilename();
       /*使用UUID生成新的文件名称*/
        String newFileName=UUID.randomUUID().toString()+oldFileName.substring(oldFileName.lastIndexOf("."));
        boolean flag=minioClient.putObject(newFileName,videoFile.getInputStream());
//        newFileName=url+newFileName;
        if(flag){
            return Result.ok(newFileName);
        }
        return Result.error();
    }


}